#include <math.h>
#include <stdio.h>

typedef struct Array {
    void* data;
    unsigned long size;
    int sparse;
    const unsigned long* idx;
    unsigned long nnz;
} Array;

struct LangCAtomicFun {
    void* libModel;
    int (*forward)(void* libModel,
                   int atomicIndex,
                   int q,
                   int p,
                   const Array tx[],
                   Array* ty);
    int (*reverse)(void* libModel,
                   int atomicIndex,
                   int p,
                   const Array tx[],
                   Array* px,
                   const Array py[]);
};

void cartpole_dynamics_flow_map_forward_zero(double const *const * in,
                                             double*const * out,
                                             struct LangCAtomicFun atomicFun) {
   //independent variables
   const double* x = in[0];

   //dependent variables
   double* y = out[0];

   // auxiliary variables
   double v[10];

   v[0] = sin(x[2]);
   v[1] = cos(x[2]);
   v[2] = 1 / (2. + 0.2 * v[0] * v[0] + 0.0500074996250188 * v[1] * v[1]);
   v[3] = 0 - x[4];
   v[4] = v[1] * x[3];
   v[5] = 0.2 * (0 - 0.5 * x[4]);
   v[6] = v[0] * x[3];
   v[7] = v[6] * v[5];
   v[8] = v[3] * v[6];
   v[9] = 0 - v[0];
   y[2] = v[2] * (x[5] - v[0] * (0.2 * (0 - v[3]) * v[4] - x[4] * (0.2 * v[4] - v[5])) - v[1] * (1.49992500374981 * v[7] + 0.0500074996250188 * v[8] + x[4] * 0.2 * v[6])) - 9.81 * (0.0500074996250188 * v[9] * v[1] + 0.2 * v[1] * v[0]) * v[2];
   y[3] = 14.9992500374981 * v[7] - 1.49992500374981 * (9.81 * v[9] + v[1] * y[2] + v[8]);
   // dependent variables without operations
   y[0] = x[3];
   y[1] = x[4];
}

